package cn.com.tp;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Base64;

@JsonPropertyOrder(alphabetic = true)
public class CompanyApiSign {
    public String companyID;
    public String requestID;
    public String content;
    public long timestamp;


    public CompanyApiSign(String companyID, String requestID, String content, long timestamp) {
        this.companyID = companyID;
        this.requestID = requestID;
        this.content = content;
        this.timestamp = timestamp;
    }

    public static String SHA256(String data) {
        return DigestUtils.sha256Hex(data);
    }

    public static String calcSign(CompanyApiSign req, String salt) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String serializedStr = mapper.writeValueAsString(req);
        String base64Str = Base64.getEncoder().encodeToString(serializedStr.getBytes());
        String data = salt + base64Str + salt;
        return SHA256(data);
    }

}